package com.dd.utils

import com.tencent.mmkv.MMKV

class MMKVUtil private constructor(mmkv: MMKV) {
    private val _mmkv: MMKV = mmkv

    companion object {
        private var instance: MMKVUtil? = null
        fun getInstance(): MMKVUtil {
            return instance ?: synchronized(this) {
                instance ?: MMKVUtil(MMKV.defaultMMKV()).apply {
                    instance = this
                }
            }
        }
    }

    fun save(key: String, value: Boolean) {
        _mmkv.encode(key, value)
    }

    fun save(key: String, value: Int) {
        _mmkv.encode(key, value)
    }

    fun save(key: String, value: Long) {
        _mmkv.encode(key, value)
    }

    fun save(key: String, value: Float) {
        _mmkv.encode(key, value)
    }

    fun save(key: String, value: Double) {
        _mmkv.encode(key, value)
    }

    fun save(key: String, value: String?) {
        _mmkv.encode(key, value)
    }

    fun save(key: String, value: Set<String>) {
        _mmkv.encode(key, value)
    }

    fun save(key: String?, value: ByteArray?) {
        _mmkv.encode(key, value)
    }

    fun getBool(key: String?): Boolean {
        return _mmkv.decodeBool(key)
    }

    fun getBool(key: String?, defaultValue: Boolean): Boolean {
        return _mmkv.decodeBool(key, defaultValue)
    }

    fun getInt(key: String?): Int {
        return _mmkv.decodeInt(key, 0)
    }

    fun getInt(key: String?, defaultValue: Int): Int {
        return _mmkv.decodeInt(key, defaultValue)
    }

    fun getLong(key: String?): Long {
        return _mmkv.decodeLong(key, 0L)
    }

    fun getLong(key: String?, defaultValue: Long): Long {
        return _mmkv.decodeLong(key, defaultValue)
    }

    fun getFloat(key: String?): Float {
        return _mmkv.decodeFloat(key, 0.0f)
    }

    fun getFloat(key: String?, defaultValue: Float): Float {
        return _mmkv.decodeFloat(key, defaultValue)
    }

    fun getDouble(key: String?): Double {
        return _mmkv.decodeDouble(key, 0.0)
    }

    fun getDouble(key: String?, defaultValue: Double): Double {
        return _mmkv.decodeDouble(key, defaultValue)
    }

    fun getString(key: String): String? {
        return _mmkv.decodeString(key, "")
    }

    fun getString(key: String, defaultValue: String): String? {
        return _mmkv.decodeString(key, defaultValue)
    }

    fun getStringSet(key: String): Set<String>? {
        return _mmkv.decodeStringSet(key, null)
    }

    fun getStringSet(key: String, defaultValue: Set<String>): Set<String>? {
        return _mmkv.decodeStringSet(key)
    }

    fun delete(key: String) {
        _mmkv.remove(key)
    }

    fun contains(key: String): Boolean {
        return _mmkv.contains(key)
    }
}